/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.GUI;

import com.DAO.DAO;
import com.Entity.Employee;
import com.Entity.EmployeeLanguages;
import com.Entity.EmployeeTests;
import com.Entity.Language;
import java.net.URL;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.stage.Stage;

/**
 *
 * @author Vi
 */
public class EmployeePopup extends ValidationForm implements Initializable {

    @FXML
    private Button popupOk;
    @FXML
    private TextField name;
    @FXML
    private TextField surname;
    @FXML
    private ComboBox position;
    @FXML
    private ComboBox education;
    @FXML
    private ComboBox certifications;
    @FXML
    private TextField employment;
    @FXML
    private TextField date_employment;
    @FXML
    private TextField work_it;
    @FXML
    private TextField other_projects;
    @FXML
    private TextField time_other_projects;
    @FXML
    private Label error_name;
    @FXML
    private Label error_surname;
    @FXML
    private Label error_position;
    @FXML
    private Label error_education;
    @FXML
    private Label error_certifications;
    @FXML
    private Label error_employment;
    @FXML
    private Label error_date_employment;
    @FXML
    private Label error_work_it;
    @FXML
    private Label error_other_projects;
    @FXML
    private Label error_time_other_projects;
    private DAO dao;
    private Stage dialogStage;
    private List<ChangeEmployeeListener> refreshTableListeners;
    private Integer editEmployeeId;
    private Employee addedEmployee;

    @FXML
    private ComboBox chooseLanguage1;
    @FXML
    private ComboBox chooseLanguage2;
    @FXML
    private ComboBox chooseLanguage3;

    @FXML
    private ComboBox chooseLevel1;
    @FXML
    private ComboBox chooseLevel2;
    @FXML
    private ComboBox chooseLevel3;

    @FXML
    private ComboBox test1;
    @FXML
    private ComboBox test2;
    @FXML
    private ComboBox test3;
    @FXML
    private ComboBox test4;
    @FXML
    private ComboBox test5;
    @FXML
    private ComboBox test6;
    @FXML
    private ComboBox test7;

    @FXML
    private Label error1;
    @FXML
    private Label error2;
    @FXML
    private Label error3;
    @FXML
    private Label error4;
    @FXML
    private Label error5;
    @FXML
    private Label error6;
    @FXML
    private Label error7;
    @FXML
    private Label error8;
    @FXML
    private Label error9;
    @FXML
    private Label error10;
    @FXML
    private Label error11;

    @FXML
    public void okAction(ActionEvent event) {
        validationForm();
    }

    private void employeeChanged() {
        for (ChangeEmployeeListener rtl : refreshTableListeners) {
            rtl.employeeChangedEvent();
        }
    }

    private void validationForm() {
        makeList(17);
        takeDateFromForm();
        setWrongTextFields(correctField);

        printValidator(correctField);

        if (isCorrect(correctField)) {

            try {
                dao = new DAO();
                Employee emp = makeEmployee();
                if (editEmployeeId != null) {
                    emp.setIdEmployee(editEmployeeId);
                    EmployeeTests employeeTests = makeEmployeeTest(emp);
                    dao.updateEmployeeTests(employeeTests);
                    dao.updateEmployee(emp);
                    List<EmployeeLanguages> employeeLanguages = makeLanguages(emp);
                    if (employeeLanguages.size() > 0) {
                        dao.updateEmployeeLanguages(emp.getIdEmployee(), employeeLanguages);
                    }
                    dialogStage.close();
                } else {
                    dao.addEmployee(emp);
                    EmployeeTests employeeTests = makeEmployeeTest(emp);
                    dao.addEmployeeTests(employeeTests);
                    List<EmployeeLanguages> employeeLanguages = makeLanguages(emp);
                    if (employeeLanguages.size() > 0) {
                        dao.addEmployeeLanguages(employeeLanguages);
                    }
                    clearForm();
                    dialogStage.close();
                }
                employeeChanged();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private List<EmployeeLanguages> makeLanguages(Employee emp) {
        
        List<EmployeeLanguages> list = new ArrayList<>();
        
        if (!this.chooseLanguage1.getValue().toString().equals("Wybierz język")) {
            String lan = this.chooseLanguage1.getValue().toString();
            String lvl = this.chooseLevel1.getValue().toString();
            list.add(new EmployeeLanguages(lvl, new Language(getLanguageId(lan)), emp));
        }
        
        if (!this.chooseLanguage2.getValue().toString().equals("Wybierz język")) {
            String lan = this.chooseLanguage2.getValue().toString();
            String lvl = this.chooseLevel2.getValue().toString();
            list.add(new EmployeeLanguages(lvl, new Language(getLanguageId(lan)), emp));
        }
        
        if (!this.chooseLanguage3.getValue().toString().equals("Wybierz język")) {
            String lan = this.chooseLanguage3.getValue().toString();
            String lvl = this.chooseLevel3.getValue().toString();
            list.add(new EmployeeLanguages(lvl, new Language(getLanguageId(lan)), emp));
        }

        return list;
    }
    
    public int getLanguageId(String languageName)
    {
        if (languageName.equals("Angielski")) return 1;
        else if (languageName.equals("Niemiecki")) return 2;
        else if (languageName.equals("Francuski")) return 3;
        else if (languageName.equals("Hiszpański")) return 4;
        else if (languageName.equals("Rosyjski")) return 5;
        else return 0;
    }

    private EmployeeTests makeEmployeeTest(Employee emp) {
        String test1 = this.test1.getValue().toString();
        String test2 = this.test2.getValue().toString();
        String test3 = this.test3.getValue().toString();
        String test4 = this.test4.getValue().toString();
        String test5 = this.test5.getValue().toString();
        String test6 = this.test6.getValue().toString();
        String test7 = this.test7.getValue().toString();

        EmployeeTests test = new EmployeeTests(Integer.parseInt(test1), Integer.parseInt(test2), Integer.parseInt(test3), Integer.parseInt(test4), Integer.parseInt(test5), Integer.parseInt(test6), Integer.parseInt(test7), emp);

        return test;
    }

    private Employee makeEmployee() {
        String nameE = this.name.getText();
        String surnameE = this.surname.getText();
        String positionE = renamePosition(this.position.getValue().toString());
        String educationE = renameEducation(this.education.getValue().toString());
        String certi = renameCertifacations(this.certifications.getValue().toString());
        String emp = this.employment.getText();
        String date_emp = this.date_employment.getText();
        String work = this.work_it.getText();
        String other_pro = this.other_projects.getText();
        String time_other_pro = this.time_other_projects.getText();
        Employee employee = new Employee(nameE, surnameE, positionE, educationE, Double.parseDouble(certi), Double.parseDouble(emp), Date.valueOf(date_emp), Double.parseDouble(work), Integer.parseInt(other_pro), Double.parseDouble(time_other_pro));

        return employee;
    }

    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
        this.dialogStage.getIcons().add(new Image("https://cdn1.iconfinder.com/data/icons/windows-8-metro-style/512/info.png"));
    }

    public Button getPopupOkButton() {
        return popupOk;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        refreshTableListeners = new ArrayList<>();
        refreshTableListeners.add(mainPanelCotroller.getInstance());

        inputs.clear();
        inputs.add(name);
        inputs.add(surname);
        inputs.add(position);
        inputs.add(education);
        inputs.add(certifications);
        inputs.add(employment);
        inputs.add(date_employment);
        inputs.add(work_it);
        inputs.add(other_projects);
        inputs.add(time_other_projects);
        inputs.add(test1);
        inputs.add(test2);
        inputs.add(test3);
        inputs.add(test4);
        inputs.add(test5);
        inputs.add(test6);
        inputs.add(test7);

        labels.clear();
        labels.add(error_name);
        labels.add(error_surname);
        labels.add(error_position);
        labels.add(error_education);
        labels.add(error_certifications);
        labels.add(error_employment);
        labels.add(error_date_employment);
        labels.add(error_work_it);
        labels.add(error_other_projects);
        labels.add(error_time_other_projects);
        labels.add(error1);
        labels.add(error2);
        labels.add(error3);
        labels.add(error4);
        labels.add(error5);
        labels.add(error6);
        labels.add(error7);

    }

    private void clearForm() {
        this.name.setText("");
        this.surname.setText("");
        this.position.setValue("Wybierz stanowisko");
        this.education.setValue("Wybierz wykształcenie");
        this.certifications.setValue("Liczba certyfikatów");
        this.employment.setText("");
        this.date_employment.setText("");
        this.work_it.setText("");
        this.other_projects.setText("");
        this.time_other_projects.setText("");
        makeList(10);
        setWrongTextFields(correctField);
    }

    private void takeDateFromForm() {

        checkString(this.name.getText(), 0);
        checkString(this.surname.getText(), 1);
        checkString(renamePosition((String) this.position.getValue()), 2);
        checkString(renameEducation((String) this.education.getValue()), 3);
        checkString(renameCertifacations((String) this.certifications.getValue()), 4);

        checkDouble(this.employment.getText(), 5);
        checkDate(this.date_employment.getText(), 6);
        checkDouble(this.work_it.getText(), 7);
        checkInteger(this.other_projects.getText(), 8);
        checkDouble(this.time_other_projects.getText(), 9);

        checkString(this.test1.getValue().toString(), 10);
        checkString(this.test2.getValue().toString(), 11);
        checkString(this.test3.getValue().toString(), 12);
        checkString(this.test4.getValue().toString(), 13);
        checkString(this.test5.getValue().toString(), 14);
        checkString(this.test6.getValue().toString(), 15);
        checkString(this.test7.getValue().toString(), 16);

    }

    public void loadDate(Integer selectedE) {

        dao = new DAO();
        Employee empl = dao.getEmployee(selectedE);
        EmployeeTests tests = dao.getEmployeeTest(empl.getIdEmployee());
        List<EmployeeLanguages> languages = dao.getEmployeeLanguages(empl.getIdEmployee());

        if (empl != null & tests != null) {
            this.name.setText(empl.getName());
            this.surname.setText(empl.getSurname());
            this.position.setValue(namePosition(empl.getPosition().toString()));
            this.education.setValue(nameEducation(empl.getEducation().toString()));
            this.certifications.setValue(nameCertifications(empl.getCertifications().toString()));
            this.employment.setText(empl.getEmployment().toString());
            this.date_employment.setText(empl.getDateEmployment().toString());
            this.work_it.setText(empl.getWorkIt().toString());
            this.other_projects.setText(empl.getOtherProjects().toString());
            this.time_other_projects.setText(empl.getTimeOtherProjects().toString());
            this.test1.setValue(tests.getPunctuality());
            this.test2.setValue(tests.getPlanning());
            this.test3.setValue(tests.getDecisionMaking());
            this.test4.setValue(tests.getManagementPeople());
            this.test5.setValue(tests.getCommunications());
            this.test6.setValue(tests.getCreativity());
            this.test7.setValue(tests.getResistanceStress());
            
            for (int i = 0; i < languages.size(); i++)
            {
                if (i == 0) 
                {
                    this.chooseLanguage1.setValue(languages.get(i).getIdLanguage().getName());
                    this.chooseLevel1.setValue(languages.get(i).getLevel());
                }
                else if (i == 1)
                {
                    this.chooseLanguage2.setValue(languages.get(i).getIdLanguage().getName());
                    this.chooseLevel2.setValue(languages.get(i).getLevel());
                }
                else if (i == 2)
                {
                    this.chooseLanguage3.setValue(languages.get(i).getIdLanguage().getName());
                    this.chooseLevel3.setValue(languages.get(i).getLevel());
                }
            }

        }
    }

    public Integer getEditEmployeeId() {
        return editEmployeeId;
    }

    public void setEditEmployeeId(Integer editEmployeeId) {
        this.editEmployeeId = editEmployeeId;
    }
    
    public void setButton(String classStyle, String text) {
        popupOk.getStyleClass().add(classStyle);
        popupOk.setText(text);
    }

}
